/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "glyph.h"

namespace tri {

TGlyph::TGlyph() :
	tri::TEntity() 
	,clip_insets(TRectangle(0,0,0,0)),_reflect(false){
	
	_bgcolor.setValue(0, 0, 0, 0);
	
}

TGlyph::~TGlyph() {
}


void TGlyph::display() {
	
	tri::graphics::TColor _color = getMaterialPointer()->getColor();
	int w = -1; 
	int h=-1;
	tri::TTexture* tex = getMaterialPointer()->getTexture();
	if (tex != NULL){
		tex->bind();
		_tc = tex->getTC();
		w = tex->getWidth();
		h = tex->getHeight();
	}else {
		unbindAllTextures();
	}

	float v[3];
	TVector size = getSize();
	v[0]  = size.x;
	v[1]  = size.y;
	v[2]  = size.z;
	
	_tc.value[0] = _tc.value[2] * ( mImageInsets.getLeft()/ w);
	_tc.value[1] = _tc.value[3] * ( mImageInsets.getTop()/ h);
	_tc.value[2] -= _tc.value[2] * ( mImageInsets.getRight()/ w);
	_tc.value[3] -= _tc.value[3] * ( mImageInsets.getBottom()/ h);
	for(int i = 0;i<4;i++){
		clip_insets.value[i] = clip_insets.value[i]<0?0 :clip_insets.value[i];
	}
	if((clip_insets.value[0] + clip_insets.value[2]) >= v[0] || (clip_insets.value[1] + clip_insets.value[3]) >= v[1]){
		return;
	}
	
	tri::graphics::renderGlyph(v[0], v[1], &_color, NULL, NULL, NULL, &_tc,0,0,0,
			clip_insets.value[0], clip_insets.value[1], clip_insets.value[2],
			clip_insets.value[3]);

//	if (!tri::display_engine::isPicking() && _reflect) {
//
//		float v2[4];
//		_tc.get(v2);
//		v2[1] = v2[3];
//		v2[3] = v2[1]*0.75;
//		tri::TRectangle rect(v2);
//		tri::graphics::TColor _f_color(_color.getRed(), _color.getGreen(),
//				_color.getBlue(), (int) ( (float)_color.getAlpha() * 0.8) );
//		tri::graphics::renderGlyph(v[0], v[1]/4, &_bgcolor, &_f_color,
//				&_bgcolor, &_f_color, &rect, 0, -v[1]*0.65, 0);
//	}
	
}

void TGlyph::setClipInsets(float l, float t, float r, float b) {
	clip_insets=tri::TRectangle(l, t, r, b);
}

void TGlyph::setImageInsets(float l, float t, float r, float b){
	mImageInsets=tri::TRectangle(l, t, r, b);
}

void TGlyph::setReflection(bool b) {
	_reflect = b;
}

void TGlyph::setBackgroundColor(int r, int g, int b) {
	_bgcolor.setValue(r, g, b, 0);
}

void TGlyph::update() {
	TEntity::update();

}

}

